package com.nahan.secondapplication.ImitateDouYin;

import android.content.Context;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;


/**
 * 图标加载工具类
 */
public class GlideUtils {

    private static String getImageUrl(String url) {
        if (!TextUtils.isEmpty(url) && url.startsWith("http")) {

            return url;
        } else {

            return "http://"+url;
        }

    }

    /**
     * 默认加载方式
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadImage(Context context, String url, ImageView imageView) {
        RequestOptions requestOptions = new RequestOptions()
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .dontAnimate();

        Glide.with(context)
                .load(getImageUrl(url))
                .apply(requestOptions)
                .into(imageView);
    }



    public static void loadImage(Context context, int url, ImageView imageView) {
        RequestOptions requestOptions = new RequestOptions()
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .dontAnimate();

        Glide.with(context)
                .load(url)
                .apply(requestOptions)
                .into(imageView);
    }


    /**
     * 图片加载错误
     * @param context
     * @param url
     * @param imageView
     * @param errorurl
     */
    public static void loadErrorImage(Context context, String url, ImageView imageView, int errorurl) {
        RequestOptions requestOptions = new RequestOptions()
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(errorurl)
                .dontAnimate();

        Glide.with(context)
                .load(getImageUrl(url))
                .apply(requestOptions)
                .into(imageView);
    }


}
